package C013;

/**
 * ClassName: Queue02
 * Package: C013
 * Description:
 *
 * @Author BCXJ
 * @Create 2024/12/2 14:12
 * @Version 1.0
 * @Since 1.0
 */
public class Queue02 {
    public static void main(String[] args) {
        MyQueue<Integer> queue = new MyQueue<>(4);

        System.out.println(queue.isEmpty());
        queue.offer(11);
        queue.offer(10);
        queue.offer(12);
        queue.offer(13);
        queue.offer(13);
        System.out.println(queue.peek());
        System.out.println(queue.poll());
        System.out.println(queue.peek());
        queue.offer(14);
        System.out.println("-------");
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
        System.out.println(queue.poll());
    }

    static class MyQueue<T>{
        Object[] arr;
        int l, r, size, limit;

        public MyQueue(int limit) {
            arr = new Object[limit];
            this.limit = limit;
        }

        public boolean isEmpty() {
            return size == 0;
        }

        public boolean isFull() {
            return size == limit;
        }

        public boolean offer(T ele) {
            if(isFull()) {
                return false;
            }
            arr[r++] = ele;
            r = r == limit ? 0 : r;
            size++;
            return true;
        }

        public T poll() {
            if(isEmpty()) {
                return null;
            }
            T ele = (T) arr[l ++];
            l = l == limit ? 0 : l;
            size --;
            return ele;
        }

        public T peek() {
            if(isEmpty()) {
                return null;
            }
            return (T) arr[l];
        }

        public int size() {
            return size;
        }
    }
}
